<?php
namespace app\common\model;


class Qqs extends Base
{
    public static function search($uid='')
    {
        $page = input('post.page/d');
        $pageSize = input('post.pageSize/d');
        $page = ($page < 1) ? 1 : $page;
        $pageSize = ($pageSize < 1 || $pageSize > 50) ? 10 : $pageSize;
        $self = new static();

		$query = $self->alias('a');
        $query->where('a.web_id', '=', config('web_web_id'));
        !empty($uid) && $query->where('a.uid', '=', $uid);
        $status = input('post.cookiezt');
        is_numeric($status) && $query->where('a.cookiezt', '=', $status);
        $query2 = clone $query;
        return [
            'total' => $query2->count('a.qq'),
            'list' => $query->page($page, $pageSize)->order('a.time desc')->select()
        ];
    }
	
	public static function updateByqid($qid, $data = [])
    {
        $self = new static();
        return ($self->where('qid', '=', $qid)
                ->update($data) !== false);
    }
	
    public static function delByqid($id)
    {
        $self = new static();
        if ($self->where('qid', '=', $id)->where('web_id', '=', config('web_web_id'))->delete()) {
            return true;
        }
        return false;
    }

    public static function findByqid($qid)
    {
        $self = new static();
        if ($result = $self
            ->where('qid', '=', $qid)
            ->find()) {
            return $result;
        } else {
            return false;
        }
    }
	
	public static function findByuin($uin)
    {
        $self = new static();
        if ($result = $self
            ->where('qq', '=', $uin)
            ->find()) {
            return $result;
        } else {
            return false;
        }
    }

    public static function getMyQqCount($uid)
    {
        $self = new static();
        $result = $self->where('web_id', '=', config('web_web_id'))
            ->where('uid', '=', $uid)
            ->count();
        return $result;
    }
	
	public static function getMyQqList($uid,$page,$pageSize)
    {
        $self = new static();
		$query = $self->alias('a');
        $query->where(['a.web_id'=>config('web_web_id'),'a.uid'=>$uid]);
		if(input('get.mod') == 1){
			$query->where('a.cookiezt', '=', 0);
		}if(input('get.mod') == 2){
			$query->where('a.cookiezt', '=', 1);
		}
		$qq = input('get.search');
		is_numeric($qq) && $query->where('a.qq', '=', $qq);
        $query2 = clone $query;
        return [
            'total' => $query2->count('a.qid'),
            'list' => $query->page($page, $pageSize)->order('a.time desc')->select()
        ];
    }

    public static function isQq($qid)
    {
        $self = new static();
        if ($self
            ->where('qid', '=', $qid)
            ->find()
        ) {
            return true;
        } else {
            return false;
        }
    }

    public static function isQqZt($qid)
    {
        $self = new static();
        if ($self
            ->where('qid', '=', $qid)
            ->where('cookiezt', '=', 0)
            ->find()
        ) {
            return true;
        } else {
            return false;
        }
    }

    public static function madd($data, $userInfo)
    {
        $self = new static();
        $uin = $data['qq'];
		$area = $data['area'];
        $pwd = isset($data['qpwd']) ? $data['qpwd'] : '';
        $sid = isset($data['qsid']) ? $data['qsid'] : 'NULL';
        $skey = $data['skey'];
        $pskey = $data['pskey'];
        $superkey = $data['superkey'];
        $pwd = authcode($pwd, 'ENCODE', WEB_KEY);
		$type = $data['type'];
        $row = $self->where(['qq' => $uin, 'web_id' => WEB_ID])->find();
        if ($row) {
            $update = [
                'cookiezt' => 0,
                'uid' => $userInfo['uid'],
                'pwd' => $pwd,
                'sid' => $sid,
				'area' => $area,
                'skey' => $skey,
                'pskey' => $pskey,
                'superkey' => $superkey
            ];
            $self->where('qid', '=', $row['qid'])
                ->update($update);
            Qqjob::where('qid', '=', $row['qid'])
                ->update(['cookiezt' => 0]);
            Qqjob::where(['qid' => $row['qid'], 'type' => 'auto'])->update([
                'lasttime' => date('m-d H:s:i'),
                'date' => date('m-d H:s:i'),
                'data' => '正常在线中'
            ]);
            $result = [
                'code' => 0,
                'message' => 'QQ状态更新成功！',
                'qq' => $uin
            ];
        } else {
            if ($userInfo['peie'] > Qqs::where(['qq' => $userInfo['qq'], 'web_id' => WEB_ID])->count()) {
                $add['web_id'] = WEB_ID;
                $add['uid'] = $userInfo['uid'];
                $add['qq'] = $uin;
                $add['pwd'] = $pwd;
                $add['sid'] = $sid;
				$add['area'] = $area;
                $add['skey'] = $skey;
                $add['pskey'] = $pskey;
                $add['superkey'] = $superkey;
				$add['type'] = $type;
                $add['time'] = date("m-d H:i:s");
                $self->insert($add);
                $qqs = $self->where('qq', '=', $uin)->find();
                Qqjob::insert([
                    'zid' => WEB_ID,
                    'uid' => $userInfo['uid'],
                    'qid' => $qqs['qid'],
                    'qq' => $uin,
                    'type' => 'auto',
                    'data' => '正常运行中',
                    'gnid' => 0,
                    'lasttime' => date('m-d H:s:i'),
                    'date' => date('m-d H:s:i')
                ]);
				Qqjob::insert([
                    'zid' => WEB_ID,
                    'uid' => $userInfo['uid'],
                    'qid' => $qqs['qid'],
                    'qq' => $uin,
					'type' => 'zan',
                    'server' => 1,
					'start' => 1,
					'xy' => 1,
					'mode' => 0,
					'lasttime' => '01-01 00:00:00',
					'nexttime' => time()
                ]);
                if (config('sys_is_addqq_msg') == 1) {
                    $sub = config('web_webname') . '- 添加QQ提醒';
                    $msg = get_mail_tempale(3, $userInfo, '');
                    send_mail($userInfo['mail'], $sub, $msg);
                }
                $result = [
                    'code' => 1,
                    'message' =>	'添加QQ成功'
                ];
            } else {
                $result = [
                    'code' => -1,
                    'message' => '您的账号配额不足，添加失败'
                ];
            }

        }
        return $result;
    }
	
	public static function listhtml($uid='')
    {
        $page = input('post.page/d');
        $pageSize = input('post.pageSize/d');
        $page = ($page < 1) ? 1 : $page;
        $pageSize = ($pageSize < 1 || $pageSize > 50) ? 10 : $pageSize;
        $self = new static();

		$query = $self->alias('a');
        $query->where('a.web_id', '=', config('web_web_id'));
        !empty($uid) && $query->where('a.uid', '=', $uid);
        $status = input('get.cookiezt');
        is_numeric($status) && $query->where('a.cookiezt', '=', $status);
		 $qq = input('get.search');
        !empty($qq) && $query->where('a.qq', '=', $qq);
        $query2 = clone $query;
		$list = '';
        foreach ($query->page($page, $pageSize)->order('a.time desc')->select() as $row) {
                    $row['cookiezt'] = cookieZt($row['cookiezt']);
                    if ($row['cookiezt'] == '正常') {
                        $color = 'lv';
						$icon = 'icon icon-squarecheck';
                    } else {
                        $color = 'hong';
						$icon = 'icon icon-squarecheck';
                    }
					$name =  get_qqnick($row['qq']);
                   $list .= '<div class="swiper-container h12 del7784 swiper-container-horizontal swiper-container-free-mode swiper-container-android"><div class="swiper-wrapper h"><a class="swiper-slide t-v swiper-slide-active" href="javascript:;" onclick="x.mode(\''.url('mobile/qqlist',['act'=>'set']).'?qq=' . $row['qq'] . '\',\'qqset\');"><div class="w10 h t-v t-c"><img src="//q4.qlogo.cn/headimg_dl?dst_uin=' . $row['qq'] . '&amp;spec=100" class="b b-hui r5 w7 h7"></div><div class="w65 h t-v bb"><div class="w25 f5"><ol class="w25"><li class="w15 f2 hui nowrap">' . $name . '</li><li class="dzi">' . $row['qq'] . '</li></ol></div><div class="w20 t-l bold nowrap qqzt3774"><font class="' . $color . '"><i class="' . $icon . ' mr1"></i>'.$row['cookiezt'].'</font></div><div class="w20 t-r pr2 hui">' . $row['time'] . '</div></div></a><div class="swiper-slide t-v swiper-slide-next"><span class="wh bg-lv t-v t-c" onclick="mode_qq_add(\'qqlist\',\'' . $row['qid'] . '\');"><i class="icon icon-update f10"></i></span><span class="wh bg-hong t-v t-c" onclick="ajax_qq_del(\'qqlist\',\'' . $row['qid'] . '\');"><i class="icon icon-delete f10"></i></span></div></div></div>		';
				 }
				if(!$list){
				if($qq==''){
				$list = '<div class="w h14 bg-bai t-v t-c f10 bb">该分类下还没有QQ哦</div>';
				}else{
				$list = '<div class="w h14 bg-bai t-v t-c f10 bb">未查询到该QQ</div>';
				}
				}
        return [
            'total' => $query2->count('a.qid'),
            'list' => $list
        ];
    }

    public static function delbyUin($uin)
    {
        $self = new static();
        if ($result = $self->where('qq', '=', $uin)->delete()) {
            return $result;
        } else {
            return false;
        }
    }
	
}